package com.example.commuity.recommend


import android.util.Log
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SizeUtils
import com.example.common.mvvm.BaseMvvmFragment
import com.example.commuity.R
import com.example.commuity.adapter.RecommendAdapter
import com.example.commuity.databinding.FragmentRecommendBinding
import com.example.commuity.uistate.CommunityUiState
import com.example.commuity.viewmodel.CommunityViewModel
import com.example.entity.BaseEntity
import com.example.recyclerview.RecyclerItemDecoration
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import dagger.hilt.android.AndroidEntryPoint
import kotlin.math.log

@AndroidEntryPoint
class RecommendFragment : BaseMvvmFragment<FragmentRecommendBinding>(){

    private val mViewModel : CommunityViewModel by activityViewModels()

    private lateinit var recommendAdapter: RecommendAdapter

    private var isLoadMore =false

    companion object {
        @JvmStatic
        fun newInstance() = RecommendFragment()
    }

    override fun getLayoutId(): Int =R.layout.fragment_recommend

    override fun initViews() {
        //初始化Recyview
        mDataBinding.rvRecommend.setHasFixedSize(true)
        val layoutManger=StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)

        //设置分割线
        layoutManger.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
        mDataBinding.rvRecommend.layoutManager= layoutManger
        mDataBinding.rvRecommend.addItemDecoration(RecyclerItemDecoration(0,SizeUtils.dp2px(8f),SizeUtils.dp2px(5f),SizeUtils.dp2px(5f)))


        //设置适配器
        recommendAdapter= RecommendAdapter()
        mDataBinding.rvRecommend.adapter=recommendAdapter

        //下拉刷新加载更多
        mDataBinding.refreshLayout.setRefreshHeader(ClassicsHeader(requireContext()))
        mDataBinding.refreshLayout.setRefreshFooter(ClassicsFooter(requireContext()))
        //下拉加载刷新更多
        mDataBinding.refreshLayout.setOnRefreshListener {
            isLoadMore=false
            mViewModel.loadRecommendData(isLoadMore)
        }
        mDataBinding.refreshLayout.setOnLoadMoreListener {
            isLoadMore=true
            mViewModel.loadRecommendData(isLoadMore)
        }

        //加载数据
        lazyLoadData()
    }

    override fun lazyLoadData() {
        if (recommendAdapter.data.size==null || recommendAdapter.data.size==0){
            mViewModel.loadRecommendData()
        }else{
            mDataBinding.rvRecommend.scrollToPosition(0)
        }

    }

    override fun setDataObserver() {
        mViewModel.mLiveData.observe(this){
            when(it){
                is CommunityUiState.loadRecommendSuccess->{upDataUi(it.data as MutableList<BaseEntity>)}
                is CommunityUiState.loadRecommendFail->(it.errorMsg)
                else->{}
            }
        }
    }

    private fun upDataUi(list: MutableList<BaseEntity>) {

        Log.i("+++++++===+++++++", "upDataUi: ${GsonUtils.toJson(list)}")

        if (!isLoadMore){
            recommendAdapter.setNewInstance(list)
            if (mDataBinding.refreshLayout.isRefreshing){
                mDataBinding.refreshLayout.finishRefresh()
            }
        }else{
            recommendAdapter.addData(list)
            if (mDataBinding.refreshLayout.isLoading){
                mDataBinding.refreshLayout.finishLoadMore()
            }

        }
    }


}